import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { HttpClient } from '@angular/common/http';
import { SimpleAnimate } from '../../../share/animation/my.simple.animation';

import { HttpService } from '../../../share/http/http.service';
import { NoticeService } from '../../../share/service/notice.service';

import { Grid } from '../../../share/model/grid.model';

import { ConfirmDialogService } from '../../../share/service/confirm.service';

@Component({
  selector: 'app-demo-list',
  templateUrl: './demo.list.component.html',
  styleUrls: ['./demo.list.component.css'],
  animations: [SimpleAnimate]
})
export class DemoListComponent implements OnInit {

  private columns = [
    { title: '', field: 'id', hidden: true },
    { title: '商品名称', field: 'goodsName' },
    {
      title: '商品图片', field: 'goodsImageUrl',
      formatter: function (row: any): any {
        return '<img width="30" height="30" src="' + row.goodsImageUrl + '">';
      }
    },
    { title: '过期日期', field: 'expirationDate' },
    {
      title: '商品价格', field: 'preSalePrice',
      formatter: function (row: any): any {
        return row.preSalePrice ? row.preSalePrice / 100 : 0;
      }
    },
    { title: '容量规格', field: 'capacity' },
    { title: '总库存', field: 'totalStock' },
    { title: '已售', field: 'soldStock' },
    {
      title: '剩余库存', field: 'other',
      formatter: function (row: any): any {
        return row.totalStock || 0 - row.soldStock || 0;
      }
    },
  ];

  grid: Grid = new Grid(this.httpService, this.noticeService, this.confirmDialogService, this.router);

  searchBody = { goodsName: '' };

  constructor(
    private httpService: HttpService,
    private noticeService: NoticeService,
    private confirmDialogService: ConfirmDialogService,
    private router: Router) { }

  ngOnInit() {
    this.initGrid();
  }

  add() {
    this.grid.add('/main/demo/edit');
  }

  edit() {
    this.grid.edit('/main/demo/edit');
  }

  remove() {
    this.grid.remove('goods/delete/');
  }

  search() {
    this.grid.reload();
  }

  private initGrid() {
    let options = {
      url: 'goods/list',
      columns: this.columns,
      searchBody: this.searchBody
    };
    this.grid.initOptions(options);
    this.search();
  }

}
